package mk.jug.academy.controller;

import java.text.MessageFormat;
import java.util.Properties;
import java.util.logging.Logger;

import mk.jug.academy.factory.EventFactory;
import mk.jug.academy.model.Page;
import mk.jug.academy.model.Supporter;
import mk.jug.academy.service.EventService;
import mk.jug.academy.service.SupporterService;
import mk.jug.academy.util.Utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/** View layer coordinator for the {@link Supporter} related functionality in the backend. */
@Controller
public class SupporterAdminController extends BaseController {
	
	private static final Logger LOG = Logger.getLogger(SupporterAdminController.class.getName());
	
	//TODO: Externalize this.
	public static final int itemsPerPage = 8;
	
	@Autowired private SupporterService supporterService;
	@Autowired private EventService eventService;
	@Autowired private Properties properties;
	
	private static String LIST_PENDING = "admin.supporters.pending";
	private static String LIST_ACTIVE = "admin.supporters.active";
	private static String LIST_REJECTED = "admin.supporters.rejected";
	private static String VIEW_SUPPORTER = "admin.supporters.view";
	
	@RequestMapping("/admin/supporters/accept.do")
	public String accept(final ModelMap map, final @RequestParam("id") Long id, final @RequestParam("redirectURL") String redirectURL) {
		return new ControllerContext(map) {
			public String execute() {
				Supporter supporter = supporterService.load(id);
				supporterService.activateSupporter(supporter);
				LOG.fine("Supporter [" + supporter.getCode() + "] has been activated.");
				eventService.createEvent(EventFactory.acceptSupporterEvent(supporter));
				String subject = properties.getProperty("activation.supporter.email.subject");
				String body = MessageFormat.format(properties.getProperty("activation.supporter.email.body"), new Object[] {supporter.getDisplayName()});
				Utils.sendMail(new String[] {supporter.getEmail(), supporter.getDisplayName()}, subject, body);
				return "redirect:" + redirectURL;
			}
		}.result();
	}
	
	@RequestMapping("/admin/supporters/reject.do")
	public String reject(final ModelMap map, final @RequestParam("id") Long id, final @RequestParam("redirectURL") String redirectURL) {
		return new ControllerContext(map) {
			public String execute() {
				Supporter supporter = supporterService.load(id);
				supporterService.deleteSupporter(supporter);
				LOG.fine("Supporter [" + supporter.getCode() + "] has been rejected.");				
				return "redirect:" + redirectURL;
			}
		}.result();
	}
	
	@RequestMapping("/admin/supporters/view.do")
	public String view(final ModelMap map, final @RequestParam("id") Long id) {
		return new ControllerContext(map) {
			public String execute() {
				Supporter supporter = supporterService.load(id);
				map.addAttribute("supporter", supporter);
				return VIEW_SUPPORTER;
			}
		}.result();
	}
	
	@RequestMapping("/admin/supporters/pending.do")
	public String pending(final ModelMap map, final @RequestParam(value = "page", required = false) Integer p, final @RequestParam(value = "highlightID", required = false) String highlightID) {
		return new ControllerContext(map) {
			public String execute() {
				Integer page = p;
				if (page == null) {
					page = 1;
				}
				Page<Supporter> pagedSupporters = supporterService.listSignedUp(page, itemsPerPage);
				map.addAttribute("supporters", pagedSupporters.getItems());
				map.addAttribute("pages", Utils.getPageNumber(itemsPerPage, pagedSupporters.getTotalCount()));
				map.addAttribute("page", page);
				map.addAttribute("highlightID", highlightID);
				return LIST_PENDING;
			}
		}.result();
	}
	
	@RequestMapping("/admin/supporters/active.do")
	public String active(final ModelMap map, final @RequestParam(value = "page", required = false) Integer p, final @RequestParam(value = "highlightID", required = false) String highlightID) {
		return new ControllerContext(map) {
			public String execute() {
				Integer page = p;
				if (page == null) {
					page = 1;
				}
				Page<Supporter> pagedSupporters = supporterService.listActive(page, itemsPerPage);
				map.addAttribute("supporters", pagedSupporters.getItems());
				map.addAttribute("pages", Utils.getPageNumber(itemsPerPage, pagedSupporters.getTotalCount()));
				map.addAttribute("page", page);
				map.addAttribute("highlightID", highlightID);
				return LIST_ACTIVE;
			}
		}.result();
	}

	@RequestMapping("/admin/supporters/rejected.do")
	public String rejected(final ModelMap map, final @RequestParam(value = "page", required = false) Integer p, final @RequestParam(value = "highlightID", required = false) String highlightID) {
		return new ControllerContext(map) {
			public String execute() {
				Integer page = p;
				if (page == null) {
					page = 1;
				}
				Page<Supporter> pagedSupporters = supporterService.listDeleted(page, itemsPerPage);
				map.addAttribute("supporters", pagedSupporters.getItems());
				map.addAttribute("pages", Utils.getPageNumber(itemsPerPage, pagedSupporters.getTotalCount()));
				map.addAttribute("page", page);
				map.addAttribute("highlightID", highlightID);
				return LIST_REJECTED;
			}
		}.result();
	}	
	
}